/*
    Matali Physics Demo
    Copyright (c) 2011 KOMIRES Sp. z o. o.
 */
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Komires.MataliPhysics;

namespace MataliPhysicsDemo
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Heightmap
    {
        Demo demo;
        PhysicsScene scene;
        string instanceIndexName;

        public Heightmap(Demo demo, int instanceIndex)
        {
            this.demo = demo;
            instanceIndexName = " " + instanceIndex.ToString();
        }

        public void Initialize(PhysicsScene scene)
        {
            this.scene = scene;
        }

        public static void CreateShapes(Demo demo, PhysicsScene scene, float margin, bool dynamic)
        {
            ShapePrimitive shapePrimitive = null;
            Shape shape = null;

            Texture2D heightmapHeights = demo.Textures["DefaultHeights"];
            Texture2D heightmapFrictions = demo.Textures["DefaultFrictions"];
            Texture2D heightmapRestitutions = demo.Textures["DefaultRestitutions"];

            shapePrimitive = scene.Factory.ShapePrimitiveManager.Create("Heightmap");
            shapePrimitive.CreateHeightmap(heightmapHeights, heightmapFrictions, heightmapRestitutions, true, dynamic);

            shape = scene.Factory.ShapeManager.Create("Heightmap");
            shape.Set(shapePrimitive, Matrix.Identity, margin);
            shape.CreateMesh();

            if (!demo.Meshes.ContainsKey("Heightmap"))
                demo.Meshes.Add("Heightmap", new DemoMesh(demo, shape, demo.Textures["Ground"], Vector2.One, true, true, false, false, true, RasterizerState.CullClockwise, dynamic));
        }

        public void Create(Vector3 objectPosition, Vector3 objectScale, Quaternion objectOrientation)
        {
            Shape heightmapShape = scene.Factory.ShapeManager.Find("Heightmap");

            PhysicsObject objectRoot = scene.Factory.PhysicsObjectManager.Create("Heightmap " + instanceIndexName);
            objectRoot.EnableCursorInteraction = true;
            objectRoot.DrawPriority = 2;
            objectRoot.Shape = heightmapShape;
            objectRoot.UserTagStr = "Heightmap";
            objectRoot.InitLocalTransform.SetPosition(ref objectPosition);
            objectRoot.InitLocalTransform.SetScale(ref objectScale);
            objectRoot.InitLocalTransform.SetOrientation(ref objectOrientation);

            objectRoot.InternalControllers.CreateHeightmapController(true);

            scene.UpdateFromInitLocalTransform(objectRoot);
        }
    }
}
